package com.fxwater.websocket.init;

import cn.hutool.core.collection.CollectionUtil;
import com.fxwater.websocket.domain.RedisCacheInfo;
import com.fxwater.websocket.domain.RedisCacheManager;
import com.fxwater.websocket.service.IRedisCacheInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Description:
 * @Author: 王明朝
 * @Date: 2019/8/17 10:13
 */
@Component
public class SceneCacheInitializer  implements ApplicationListener {

    @Autowired
    private IRedisCacheInfoService redisCacheInfoService;

    private Logger logger =  LoggerFactory.getLogger(ScheduledTaskInitializer.class);

    @Override
    public void onApplicationEvent(ApplicationEvent applicationEvent) {
        logger.info("开始初始化场景缓存信息");
        List<RedisCacheInfo> redisCacheInfos = redisCacheInfoService.selectAll();
        if(CollectionUtil.isNotEmpty(redisCacheInfos)){
            redisCacheInfos.forEach(redisCacheInfo -> RedisCacheManager.getInstance().registCacheInfo(redisCacheInfo));
        }
        logger.info("结束初始化场景缓存信息");
    }
}
